﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Common
{
    /// <summary>
    /// 字符串验证工具
    /// </summary>
    public static class StringValidHelper
    {

        /**/
        ///  <summary> 
        /// 过滤sql中非法字符 
        ///  </summary> 
        ///  <param name="value">要过滤的字符串 </param> 
        ///  <returns>string </returns> 
        public static string Filter(string value)
        {
            if (string.IsNullOrEmpty(value)) return string.Empty;
            value = Regex.Replace(value, @";", string.Empty);
            value = Regex.Replace(value, @"'", string.Empty);
            value = Regex.Replace(value, @"&", string.Empty);
            value = Regex.Replace(value, @"%20", string.Empty);
            value = Regex.Replace(value, @"--", string.Empty);
            value = Regex.Replace(value, @"==", string.Empty);
            value = Regex.Replace(value, @" <", string.Empty);
            value = Regex.Replace(value, @">", string.Empty);
            value = Regex.Replace(value, @"%", string.Empty);

            return value;
        }

        /// <summary>
        /// 判断字符串中是否包含中文
        /// </summary>
        /// <param name="str">需要判断的字符串</param>
        /// <returns>判断结果</returns>
        public static bool HasChinese(this string str)
        {
            return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
        }

        /// <summary>
        /// 判断字符串长度是否溢出
        /// 分含中文长度与不含中文长度
        /// </summary>
        /// <param name="str"></param>
        /// <param name="maxCnLength"></param>
        /// <param name="maxNoCnLength"></param>
        /// <returns></returns>
        public static bool CheckOverLengthIncludeChinese(this string str, int maxCnLength, int maxNoCnLength)
        {
            if (str.HasChinese())
            {
                if (str.Length > maxCnLength)
                {
                    return true;
                }
            }
            else
            {
                if (str.Length > maxNoCnLength)
                {
                    return true;
                }
            }

            return false;
        }
    }

}
